import React from 'react';
import {
    Row,
    Col,
    Table,
    Button,
    Modal,
    message
} from 'antd';
import axios from 'axios';
import intercept from '../../../utils/intercept';
import { config } from 'utils';
import { Input } from 'antd';
import "../common.less";

const confirm = Modal.confirm;
export default class passwordResert extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            dataSource: [],
            loading: false,
        };
        //console.log(props)
    };
    componentDidMount() {

    };

    search(value) {
        this.setState({ loading: true });
        this.QueryRequest(value);
    }

    QueryRequest(key) {
        const token = sessionStorage.getItem("token");
        axios.get(`${config.api}User/GetWebUserInfo?key=${key}`,
            { headers: { Authorization: 'Bearer ' + token } })
            .then(data => {
                this.setState({ loading: false });
                this.setState({ dataSource: data.data.userInfos });
            })
            .catch(err => {
                intercept
            })
    }

    reset = (record) => {
        //console.log(record.userid)
        confirm({
            title: '确定重置该用户密码？',
            okText: '确定',
            okType: 'danger',
            cancelText: '取消',
            onOk() {
                let token = sessionStorage.getItem("token");
                axios.post(`${config.api}User/ResetUserPassword`,
                    { userid: record.userid, },
                    { headers: { Authorization: 'Bearer ' + token } })
                    .then((data) => {
                        if (data.data.result) {
                            message.success('该用户密码已重置！');
                        } else {
                            message.warning(data.data.errorMessage);
                        }
                    })
                    .catch(err => {
                        intercept
                    })
            },
            onCancel() {

            },
        });
    }






    render() {
        const Search = Input.Search;

        const scroll = {
            y: 600
        };
        const columns = [
            {
                title: "用户名",
                dataIndex: "userName",
                width: 200
            },
            {
                title: "手机号",
                dataIndex: "phone",
                width: 200

            },
            {
                title: "身份证号",
                dataIndex: "cardid",
                width: 200

            },
            {
                title: "平台",
                dataIndex: "type",
                width: 200
            },
            {
                title: "重置状态",
                width: 200,
                render: (text, record) => {
                    return (
                        <Button type="danger" onClick={() => { this.reset(record) }}>重置密码</Button>
                    )
                }

            },
        ];
        return (
            <div>
                <div style={{ textAlign: 'center' }}>
                    <Search
                        placeholder="请输入任意查询条件"
                        style={{
                            width: 600
                        }}
                        onSearch={value => this.search(value)} />
                </div>

                <Row gutter={8} style={{ marginTop: '20px' }}>
                    <Col span={24}>
                        <Table className="center"
                            {...this.state}
                            rowKey={record => record.userid}
                            border={true}
                            columns={columns}
                            scroll={scroll}
                        />
                    </Col>
                </Row>
            </div>
        )
    }
}
